package com.meterreadingsys.service;




import com.meterreadingsys.VO.UserVO;
import com.meterreadingsys.httpserver.HttpClientServer;

import com.meterreadingsys.util.SharedPreferenceUtil;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;


@SuppressLint({ "HandlerLeak", "HandlerLeak" })
public class GPSService extends Service {

	public boolean b = false;

	public static final String action = "com.opengps";

	@Override
	public void onDestroy() {
		System.out.println("onDestroy");
		MyHandler.removeCallbacks(runnable);
	}

	public String url;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		String[] s = SharedPreferenceUtil.queryWeb(this);
		url = "http://" + s[0] + ":" + s[1] + "gps";
		System.out.println(url);
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		System.out.println("onStart");
		getLocation();
		//MyHandler.post(runnable);
	}

	public Handler MyHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			
              Bundle bundle=msg.getData();
              UserVO user=new UserVO();
              user.setAltitude(bundle.getDouble("lot"));
              
              user.setLatitude(bundle.getDouble("log"));
              HttpClientServer server=new HttpClientServer(GPSService.this, "/Struts2/login!ckecklogin");
             String s= server.doUpdateData(user);
             if(s.equals("error")){
            	// MyHandler.removeCallbacks(runnable);            	
            	 
             }
           
		}

	};

	public Runnable runnable = new Runnable() {

		@Override
		public void run() {
//			String date=new SimpleDateFormat("HH:mm").format(new Date().getTime());
//              System.out.println(date);
//              System.out.println(date.equals("09:30"));
			getLocation();
              MyHandler.postDelayed(runnable, 10000);
		}

	};
	
	private void getLocation()
    {
        // 获取位置管理服务
        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) this.getSystemService(serviceName);
        // 查找到服务信息
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
       
        String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
        
        
        final Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
        updateToNewLocation(location);
       
       
        // 设置监听器，自动更新的最小时间为间隔N秒(1秒为1*1000，这样写主要为了方便)或最小位移变化超过N米
        locationManager.requestLocationUpdates(provider, 10 * 1000, 0,
                new LocationListener() {
					
					@Override
					public void onStatusChanged(String provider, int status, Bundle extras) {
						
						
					}
					
					@Override
					public void onProviderEnabled(String provider) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onLocationChanged(Location location) {
						updateToNewLocation(location);
						
					}
				});
    }
	private void updateToNewLocation(Location location) {

      
      
       
        if (location != null) {
            Message msg=new Message();
             Bundle bundle=new Bundle();
             System.out.println(location.getLongitude());
             bundle.putDouble("lot", location.getLatitude());
             bundle.putDouble("log", location.getLongitude());
             msg.setData(bundle);
             MyHandler.sendMessage(msg);
             
        } else {
           System.out.println("无法获取");
        }

    }
}
